function z(obj) {
	console.log(obj);
}

function get_domain() {
	var domain = window.location.host;
	var path = window.location.pathname.split('/');
	domain = domain == 'localhost' ? domain + '/' + path[1] : domain;
	return 'http://' + domain + '/';
}

function tips(msg, callback) {
	var tips = $('.my_tips');

	if (tips.length < 1) {
		$('body').append('<div class="my_tips"></div>');
		tips = $('.my_tips');
	}
	tips.html(msg).fadeIn('fast');
	var top = ($(window).height() - tips.height() - 60) / 2 - 60;
	var left = ($('body , html').width() - tips.width() - 40) / 2;

	tips.css({
		top : top + 'px',
		left : left + 'px',
		position : 'fixed',
	});

	setTimeout(function() {
		tips.fadeOut();
		if (typeof (callback) != 'undefined') {
			callback();
		}
	}, 1000);
}

var sure = {
	config : {},
	init : function(msg, ok, cancel) {
		var _this = this;
		_this.config.ok = ok;
		_this.config.cancel = cancel;

		if ($('.ios_confirm').length < 1) {
			var html = '';
			html = '<div class="ios_confirm">';
			html += '<div class="ios_confirm_body"></div>';
			html += '<div class="ios_confirm_btn">';
			html += '<button class="half" id="ios_confirm_cancel_btn" type="button" onclick="sure.cancel(); ">取消</button>';
			html += '<button class="half" id="ios_confirm_ok_btn" type="button" onclick="sure.ok(); ">确定</button>';
			html += '</div></div>';
			$('body').append(html);
		}
		if ($('.my_modal').length < 1) {
			$('body').append('<div class="my_modal"></div>');
		}
		$('.ios_confirm_body').html(msg);
		_this.show();
	},
	ok : function() {
		var _this = this;
		if (typeof (_this.config.ok) != 'undefined') {
			_this.config.ok();
		}
		_this.hide();
	},
	cancel : function() {
		var _this = this;
		if (typeof (_this.config.cancel) != 'undefined') {
			_this.config.cancel();
		}
		_this.hide();
	},
	show : function() {
		var _this = this;
		var height = $(document).height() > $(window).height() ? $(document).height() : $(window).height();
		var width = $(document).width();
		$('.my_modal').height(height - 1).show();

		var top = ($(window).height() - $('.ios_confirm').height()) / 2 - 60;
		var left = ($(window).width() - $('.ios_confirm').width()) / 2;

		$('.ios_confirm').css({
			top : top + 'px',
			left : left + 'px',
			position : 'fixed',
		});
		$('.ios_confirm').show();
	},
	hide : function() {
		var _this = this;
		$('.my_modal').hide();
		$('.ios_confirm').hide();
	},
};

var empty = function(val) {
	if (val === null || typeof (val) == 'undefined' || val == 0 || val == '' || val.length == 0) {
		return true;
	} else {
		return false
	}
}

var isset = function(val) {
	if (typeof (val) == 'undefined') {
		return false;
	} else {
		return true
	}
}

var ios_alert = {
	config : {},
	init : function(msg, callback) {
		var _this = this;
		_this.config.callback = callback

		if ($('.ios_alert').length < 1) {
			var html = '';
			html = '<div class="ios_alert">';
			html += '<div class="ios_alert_body"></div>';
			html += '<div class="ios_alert_btn">';
			html += '<button id="ios_alert_cancel_btn" type="button" onclick="ios_alert.callback(); ">知道了</button>';
			html += '</div></div>';
			$('body').append(html);
		}
		if ($('.my_modal').length < 1) {
			$('body').append('<div class="my_modal"></div>');
		}
		$('.ios_alert_body').html(msg);
		_this.show();
	},
	callback : function() {
		var _this = this;
		if (typeof (_this.config.callback) != 'undefined') {
			_this.config.callback();
		}
		_this.hide();
	},
	show : function() {
		var _this = this;
		var height = $(document).height() > $(window).height() ? $(document).height() : $(window).height();
		var width = $(document).width();
		$('.my_modal').height(height).width(width).show();

		var top = ($(window).height() - $('.ios_alert').height()) / 2 - 60;
		var left = ($(window).width() - $('.ios_alert').width()) / 2;

		$('.ios_alert').css({
			top : top + 'px',
			left : left + 'px',
			position : 'fixed',
		});

		$('.ios_alert').show();
	},
	hide : function() {
		var _this = this;
		$('.my_modal').hide();
		$('.ios_alert').hide();
	},
}

$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name] !== undefined) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};

Array.prototype.remove = function(dx) {
	if (isNaN(dx) || dx > this.length) {
		return false;
	}
	for (var i = 0, n = 0; i < this.length; i++) {
		if (this[i] != this[dx]) {
			this[n++] = this[i]
		}
	}
	this.length -= 1
}

var tip_loading = {
	show : function() {
		if ($('.my_loading').length < 1) {
			$('body').append('<div class="my_loading"></div>');
		}
		var loading_obj = $('.my_loading');
		var top = ($(window).height() - loading_obj.height()) / 2 + $(window).scrollTop();
		var left = ($(window).width() - loading_obj.width()) / 2 + $(window).scrollLeft();

		loading_obj.css({
			top : top + 'px',
			left : left + 'px',
		});
		loading_obj.fadeIn('fast');
	},
	hide : function() {
		$('.my_loading').fadeOut();
	}
}

var INLOADING = false;
function loading() {
	if ($('.my_loading').length < 1) {
		$('body').append('<div class="my_loading"></div>');
	}
	var loading_obj = $('.my_loading');

	$(document).ajaxStart(function() {
		var top = ($(window).height() - loading_obj.height()) / 2 - 100;
		var left = ($(window).width() - loading_obj.width()) / 2 + $(window).scrollLeft();

		loading_obj.css({
			top : top + 'px',
			left : left + 'px',
		});
		if (INLOADING) {
			return;
		}
		INLOADING = true;
		loading_obj.fadeIn('fast', function() {
			INLOADING = false;
		});
	}).ajaxComplete(function(event, XMLHttpRequest, ajaxOptions) {
		loading_obj.fadeOut('normal', function() {
			INLOADING = false;
		});
	});
}

function url_2_page(url) {
	var len = url.split('/').length - 1;
	return url.split('/')[len].split('.')[0];
}

function is_weixin() {
	var ua = navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		return true;
	} else {
		return false;
	}
}

var KE_Setting = {
	items : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|',
			'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent',
			'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/', 'formatblock', 'fontname',
			'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|',
			'image', 'flash', 'media', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink' ],
	uploadJson : get_domain() + 'index/upload/kindeditor',
	// fileManagerJson : getDomain() + 'api/file_manager',
	allowFileManager : true,
	urlType : 'domain',
	resizeType : 1,
	afterBlur : function() {
		this.sync();
	}
};

var DTP_DATE_OPTION = {
	language : 'zh-CN',
	format : 'yyyy-mm-dd',
	startView : 2,
	minView : 2,
	autoclose : 1,
	todayBtn : true,
	todayHighlight : true,
	pickerPosition : 'bottom-right',
};

var DTP_TIME_OPTION = {
	language : 'zh-CN',
	format : 'hh:ii',
	startView : 0,
	minView : 0,
	minuteStep : 15,
	autoclose : 1,
	// forceParse : false ,
	pickerPosition : 'bottom-left',
};

var DTP_DATETIME_OPTION = {
	language : 'zh-CN',
	format : 'yyyy-mm-dd hh:ii',
	startView : 2,
	minView : 0,
	autoclose : 1,
	todayBtn : true,
	todayHighlight : true,
};

var formToJson = function(formObj) {
	var data = formObj.serializeArray();
	var ret = {};
	$.each(data, function(index, row) {
		ret[row["name"]] = row["value"];
	});

	return ret;
}

function fillform(formobj, data) {
	formobj.find(':input').each(function() {
		var name = $(this).attr('name');
		if (isset(name) && isset(data) && isset(data[name])) {
			var val = data[name];
			switch (this.type) {
			case 'checkbox':
			case 'radio':
				$('input[name="' + name + '"][value="' + val + '"]').prop('checked', true);
				break;
			default:
				$(this).val(val);
				break;
			}
		}
	});
}

function validform(formobj) {
	var ret = true;
	formobj.find(':input').each(function() {
		var val = $.trim($(this).val());
		if ($(this).data('valid')) {
			switch ($(this).data('valid')) {
			case 'required':
				if (val == '' || val == 0 || val.length == 0) {
					ret = false;
				}
				break;
			case 'number':
				if (!$.isNumeric(val) || val < 0) {
					ret = false;
				}
				break;
			default:
				break;
			}

			if (!ret) {
				$(this).focus();
				tips($(this).data('tips'));
				$(this).parents('.form-group').addClass('has-error has-feedback');
				$(this).after('<span class="glyphicon glyphicon-remove form-control-feedback"></span>');
				return ret;
			} else {
				$(this).parents('.form-group').removeClass('has-error has-feedback');
				$(this).next('span').remove();
			}
		}
	});

	return ret;
}

function is_mutliplayer(goodstype_id, goodstype_data) {
	for (var i = 0; i < goodstype_data.length; i++) {
		if (goodstype_data[i].id == goodstype_id) {
			return goodstype_data[i].multiPlayer == 1;
		}
	}
}

function clone(obj) {
	var o, obj;
	if (obj.constructor == Object) {
		o = new obj.constructor();
	} else {
		o = new obj.constructor(obj.valueOf());
	}
	for ( var key in obj) {
		if (o[key] != obj[key]) {
			if (typeof (obj[key]) == 'object') {
				o[key] = clone(obj[key]);
			} else {
				o[key] = obj[key];
			}
		}
	}
	o.toString = obj.toString;
	o.valueOf = obj.valueOf;
	return o;
}

function ajax_post( url , data , success_callback , error_callback ){
  $.ajax({
    type     : "POST",
    url      : url ,
    data     : data ,
    dataType : 'json',
    success  : function( ret ){
      success_callback( ret );
    } , 
    error    : function( XMLHttpRequest , textStatus , errorThrown ) {
      error_callback( XMLHttpRequest , textStatus , errorThrown );
    }
  });
}



